"""
本文详细阐述了Assistant API中Message类的各项功能，涵盖了消息的创建、列举、检索、修改等操作。
"""
from dashscope import Messages
from dashscope import Threads
import json
print('创建线程')
thread = Threads.create(messages=[{"role": "user", "content": "How does AI work? Explain it in simple terms."}])
print('创建消息')
thread_id = thread.id
msg = Messages.create(thread_id,
                      content='The message content.',
                      role='user',
                      metadata={'key': 'value'})
print(json.dumps(msg, default=lambda o: o.__dict__, sort_keys=True, indent=4))
message_id = msg.id
print('消息列表')
messages = Messages.list('thread_id',
                         limit=1,
                         order='desc',
                         after='',
                         before='')
print(json.dumps(messages, default=lambda o: o.__dict__, sort_keys=True, indent=4))
print('检索消息')
msg = Messages.retrieve(message_id,
                            thread_id=thread_id)
print(json.dumps(msg, default=lambda o: o.__dict__, sort_keys=True, indent=4))
print('修改消息')
msg = Messages.update(message_id,
                    thread_id=thread_id,
                    metadata={'key': 'new_value'})
print(json.dumps(msg, default=lambda o: o.__dict__, sort_keys=True, indent=4))